package com.base.cn.platform.os.controller.manage.loreset;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.manage.loreset.LoresetPointService;
import com.base.cn.platform.os.service.manage.loreset.LoresetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/manage/point")
public class AdminLoresetPointController extends AdminBaseController {

    @Autowired
    private LoresetPointService loresetPointService;
    @Autowired
    private LoresetService loresetService;

    /**
     * 删除知识点数据
     *
     * @param ids 知识点数据
     * @return Map<String               ,               Object>
     */
    @RequestMapping("/deleteLoresetPoint")
    @ResponseBody
    public Map<String, Object> deleteLoresetPoint(@RequestParam("ids") String ids) {
        return loresetPointService.deleteLoresetPoint(ids);
    }

    /**
     * 修改知识点父级ID
     *
     * @param id       知识点ID
     * @param parentId 知识点父级ID
     * @return Map<String               ,               Object>
     */
    @RequestMapping("/updateLoresetPointParentId")
    @ResponseBody
    public Map<String, Object> updateLoresetPointParentId(@RequestParam("id") BigDecimal id, @RequestParam("parentId") BigDecimal parentId) {
        return loresetPointService.updateLoresetPointParentId(id, parentId);
    }

    /**
     * 初始化修改/添加页面
     *
     * @param model
     * @param loresetId 知识体系ID
     * @param parentId  父级ID
     * @param id        知识点ID
     * @return 操作页面
     */
    @RequestMapping(value = {"/initUpdate", "initAdd"})
    public String initUpdateOrAdd(Model model,
                                  @RequestParam(value = "loresetId", required = false, defaultValue = "0") BigDecimal loresetId,
                                  @RequestParam(value = "parentId", required = false, defaultValue = "0") BigDecimal parentId,
                                  @RequestParam(value = "id", required = false, defaultValue = "0") BigDecimal id) {
        Map<String, Object> loresetPoint = null;
        if (id.compareTo(new BigDecimal(0)) > 0) {
            loresetPoint = loresetPointService.findLoresetPointById(id);
        }
        if (loresetPoint == null) {
            loresetPoint = new HashMap<>();
            loresetPoint.put("parentId", parentId);
            loresetPoint.put("loresetId", loresetId);
        }
        model.addAttribute("loresetPoint", loresetPoint);
        return resultPage("/manage/loreset/save-point");
    }

    /**
     * 保存知识点数据对象
     *
     * @param loresetPoint 知识点数据对象
     * @return Map<String               ,               Object>
     */
    @RequestMapping("/saveLoresetPoint")
    @ResponseBody
    public Map<String, Object> saveLoresetPoint(@RequestBody Map<String, Object> loresetPoint) {
        return loresetPointService.saveLoresetPoint(loresetPoint);
    }

    /**
     * 初始设置知识体系节点的页面
     *
     * @param model
     * @param loresetId 知识体系ID
     * @return 操作页面
     */
    @RequestMapping("/toLoresetPointView")
    public String toLoresetPointView(Model model,
                                     @RequestParam("loresetId") BigDecimal loresetId) {
        model.addAttribute("loresetId", loresetId);
        Map<String, Object> loreset = loresetService.findLoresetById(loresetId, false, false, false);
        model.addAttribute("loreset", loreset);
        return resultPage("/manage/loreset/lore-point");
    }

    /**
     * 获取知识体系下的知识点数据列表
     *
     * @param loresetId 知识体系ID
     * @return Map<String               ,               Object>
     */
    @RequestMapping("/ajaxLoresetPointList")
    @ResponseBody
    public Map<String, Object> ajaxLoresetPointList(@RequestParam("loresetId") BigDecimal loresetId) {
        List<Map<String, Object>> loresetPointList = loresetPointService.findLoresetPointListByLoresetIds(loresetId.toString());
        return ResultUtil.SUCCESS(loresetPointList);
    }

    /**
     * 根据专业查询考点
     *
     * @param subjectIds 专业id
     * @param chkStyle   勾选框样式 checkbox/radio
     * @return 试题选择考点页
     */
    @RequestMapping("/findLoresetPointBySubjectPage")
    public String findLoresetPointBySubjectPage(Model model,
                                                @RequestParam("subjectIds") String subjectIds,
                                                @RequestParam(value = "chkStyle", required = false, defaultValue = "checkbox") String chkStyle) {

        Map<String, Object> params = Collections.singletonMap("subjectIds", subjectIds);
        List<Map<String, Object>> loresetList = loresetService.findLoresetList(params, false, false, true);
        model.addAttribute("loresetList", loresetList);
        model.addAttribute("chkStyle", chkStyle);
        return resultPage("/manage/loreset/select-loreset-point");
    }
}
